<template>
  <div class="drugStorage">
    <div class="drugStorage-page">
      <div class="drugStorage-page-content">
        <div class="drugStorage-page-form">
          <el-input
            style="width: 300px"
            placeholder="请输入内容"
            :suffix-icon="Search"
          />
        </div>
        <div class="drugStorage-page-table">
          <Table :columns="tableColumns" :data-source="dataSource" />
        </div>
        <div class="drugStorage-page-pagination">
          <Pagination
            style="margin-left: auto"
            :count="total"
            :page-size="pageSize"
            :pageNo="currentPage"
            @onPagechange="handlePageChange"
            @onSizechange="handleSizeChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { Search } from '@element-plus/icons-vue';
import { ElForm } from 'element-plus';
import store from '@/store';

import type { TableColumn } from '@/types/table';
import type { FormStorage, List } from '@/types/storage/index';
import type { FormInstance, FormRules } from 'element-plus';
import { getPatientExamList } from '@/api/patient';
const props = defineProps<{
  patientId: number | null;
  selectedDate: string;
}>();

// 根据selectedDate计算今天、明天、昨天的日期
const today = computed(() => {
  const date = new Date();
  return date.toISOString().split('T')[0]; // YYYY-MM-DD格式
});

const tomorrow = computed(() => {
  const date = new Date();
  date.setDate(date.getDate() + 1);
  return date.toISOString().split('T')[0];
});

const yesterday = computed(() => {
  const date = new Date();
  date.setDate(date.getDate() - 1);
  return date.toISOString().split('T')[0];
});

const dataSource = ref<any[]>([]); // 数据源
const total = ref(10); // 总条数
const currentPage = ref(1); // 当前页
const pageSize = ref(10); // 每页条数
const tableColumns: TableColumn[] = [
  // {  type:"selection", width: 50,prop:'selection', label: '选择', fixed: 'left' },
  { prop: 'orderNo', label: '订单编号' },
  { prop: 'visitNo', label: '诊号' },
  { prop: 'currentTimes', label: '当前第几次' },
  { prop: 'doctorName', label: '开单人名' },
  { prop: 'projectName', label: '项目名称', },
  { prop: 'examStatus', label: '检查状态' },
  { prop: 'auditStatus', label: '审核状态' },
  { prop: 'examEmpName', label: '检验人姓名' },
  { prop: 'examDeptName', label: '检验科室名' },
  { prop: 'examTime', label: '检验时间' },
  { prop: 'diagnosisFlag', label: '诊断意见' },
  { prop: 'examDesc', label: '检查描述', },
];

//弹窗调用
interface DialogRefType {
  open: () => void;
  handleClose: () => void;
}
const dialogRef = ref<DialogRefType | null>(null);
const title = ref<string>('');
const handlePageChange = (page: number) => {
  //console.log('当前页:', page);
  currentPage.value = page;
  getDataList(); // 获取数据列表
};
const handleSizeChange = (size: number) => {
  //console.log('每页条数:', size);
  pageSize.value = size;
  getDataList(); // 获取数据列表
};
const handleClose = (e: string) => {
  //console.log(e, '父组件方法被调用');
};
const getDataList = async () => {
  try {
    let visitDate = '';
    if (props.selectedDate == 'today') {
      visitDate = today.value ?? ''; // 使用今天的日期，确保为字符串
    } else if (props.selectedDate == 'tomorrow') {
      // 处理明天的逻辑
      visitDate = tomorrow.value ?? ''; // 使用今天的日期，确保为字符串
    } else if (props.selectedDate == 'yesterday') {
      // 处理明天的逻辑
      visitDate = yesterday.value ?? ''; // 使用今天的日期，确保为字符串
    }
    const response = await getPatientExamList({
      patientId: props.patientId,
      pageNo: currentPage.value,
      pageSize: pageSize.value,
      projectCategory: '1', // 假设这是疾病相关的查询
      visitDate: visitDate, // 使用visitDate参数
    });
    if (response && response.data) {
      total.value = response.data.totalCount; // 更新总条数
      dataSource.value = response.data.list
    }
    //console.log('患者挂号记录:', response);
  } catch (error) {
    //console.error('获取患者挂号记录失败:', error);
  }
};

onMounted(async () => {
  try {
    getDataList()
  } catch (error) {
    //console.error('获取患者挂号记录失败:', error);
  }
});
</script>

<style lang="scss" scoped>
.drugStorage {
  height: 100%;
}
.drugStorage-dialog {
  width: 80%;
  margin: 0 auto;
}
.drugStorage-page {
  background: #fff;
  border-radius: 6px;
  height: 100%;
  .drugStorage-page-nav {
    display: flex;
    align-items: center;
    justify-content: left;
    border-radius: 4px;
    border-bottom: 1px solid #e7e7e7;
    .el-dropdown-link {
      background: rgba(22, 173, 164, 0.08);
      border-radius: 0px 0px 0px 0px;
      padding: 16px 21px;
      cursor: pointer;
      color: var(--el-color-primary) !important;
      display: flex;
      font-size: 16px;
      align-items: center;
    }
  }
  .drugStorage-page-content {
    padding: 5px 24px;
    height: calc(100% - 50px);
    .drugStorage-page-form {
      display: flex;
      align-items: center;
      .drugStorage-page-form-right {
        margin-left: auto;
      }
    }
    .drugStorage-page-table {
       margin-top: 20px;
      height: 244px;
    }
    .drugStorage-page-pagination {
      margin-top: 24px;
      display: flex;
      align-items: center;
      .drugStorage-page-pagination-total {
        margin-right: auto;
        font-family: Microsoft YaHei, Microsoft YaHei;
        font-weight: 400;
        font-size: 14px;
        color: rgba(0, 0, 0, 0.6);
        text-align: left;
        font-style: normal;
        text-transform: none;
      }
    }
  }
}
</style>
